package org.benjamin.tenancy.domain.entity;

import java.math.BigDecimal;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;;

/**
 * 记住收据上的所有数据
 * 
 * @author 黄文杰
 *
 */
@Entity
@Table(name = "tab_receipt_info")
public class ReceiptBills {

	@Transient
	private static final String[] CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

	private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾",
			"佰", "仟", "兆", "拾", "佰", "仟" };

	@Transient
	private static String numberToCNMontrayUnit(BigDecimal numberOfMoney) {
		String CN_NEGATIVE = "负";
		String CN_ZEOR_FULL = "零元";
		int MONEY_PRECISION = 2;
		StringBuffer stringBuffer = new StringBuffer();
		int signum = numberOfMoney.signum();
		if (signum == 0) {
			return CN_ZEOR_FULL;
		}

		long number = numberOfMoney.movePointRight(MONEY_PRECISION).setScale(0, 4).abs().longValue();
		long scale = number % 100;
		int numUnit = 0;
		int numIndex = 0;
		boolean getZero = false;
		if (!(scale > 0)) {
			numIndex = 2;
			number = number / 100;
			getZero = true;
		}
		if ((scale > 0) && (!(scale % 10 > 0))) {
			numIndex = 1;
			number = number / 10;
			getZero = true;
		}
		int zeroSize = 0;
		while (true) {
			if (number <= 0) {
				break;
			}
			numUnit = (int) (number % 10);
			if (numUnit > 0) {
				if ((numIndex == 9) && (zeroSize >= 3)) {
					stringBuffer.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
				}
				if((numIndex == 13) && (zeroSize >= 3)) {
					stringBuffer.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
				}
				stringBuffer.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
				stringBuffer.insert(0, CN_UPPER_NUMBER[numUnit]);
				getZero = false;
				zeroSize = 0;
			}else {
				++zeroSize;
				if(!(getZero)) {
					stringBuffer.insert(0, CN_UPPER_NUMBER[numUnit]);
				}
				if(numIndex == 2) {
					if(number > 0) {
						stringBuffer.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
					}
				}else if(((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
					stringBuffer.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
				}
				getZero = true;
			}
			number = number / 10;
			++numIndex;
		}
		if(signum == -1) {
			stringBuffer.insert(0, CN_NEGATIVE);
		}
		
		if(!(signum > 0)) {
			stringBuffer.insert(0, "");
		}
		return stringBuffer.toString();
	}

	/**
	 * 编号
	 */
	@Id
	private String no;

	@Column(name = "client")
	private String client;

	/**
	 * 名称
	 */
	@Column(name = "name")
	private String name;

	/**
	 * 创建时间
	 */
	@Column(name = "create_date")
	private Date createDate;

	@OneToMany
	@JoinColumn(name = "receipt_info_id")
	private Set<ReceiptParticulars> receiptParticulars = new HashSet<>();

	/**
	 * 手机号码
	 */
	@Column(name = "phone")
	private String phone;

	@Column(name = "sum_money")
	private BigDecimal sumMoney;

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getClient() {
		return client;
	}

	public void setClient(String client) {
		this.client = client;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Set<ReceiptParticulars> getReceiptParticulars() {
		return receiptParticulars;
	}

	public void setReceiptParticulars(Set<ReceiptParticulars> receiptParticulars) {
		this.receiptParticulars = receiptParticulars;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public BigDecimal getSumMoney() {
		return sumMoney;
	}

	public void setSumMoney(BigDecimal sumMoney) {
		this.sumMoney = sumMoney;
	}

}
